package com.gome.ss.flowcrawl.plugin;

import com.gome.ss.common.AppConfiguer;
import com.gome.ss.common.FlowCrawlHbaseHandle;
import com.gome.ss.core.datapipe.DataPipeInterface;
import com.gome.ss.core.download.SpiderDataMap;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;

public class FlowCrawlDataPipe implements DataPipeInterface {
	
	private DBCollection pageCollection = null;
	private String cname = "";
	
	public FlowCrawlDataPipe(String cname) {
		this.cname = cname;
		pageCollection = AppConfiguer.flowCrawlConfigMongo.getDB(AppConfiguer.FLOWCRAWL_DB_NAME).getCollection(cname);
	}
	
	public void clean() {
		if (!AppConfiguer.flowcrawlUseHbaseTransit)
			pageCollection.drop();
	}
	
	@Override
	public void saveData(SpiderDataMap data) {
		// TODO Auto-generated method stub
		if (AppConfiguer.flowcrawlUseHbaseTransit)
			FlowCrawlHbaseHandle.insertData(cname, data);
		else {
			DBObject obj = (BasicDBObject) JSON.parse(data.toJson());
			pageCollection.insert(obj);
		}
	}
}
